import { ResultType } from '@/types/index'
import {
  getUserInfo,
  getUserInfoByCode,
  saveUserInfo,
  deleteUserInfo,
  getGradeInfo,
  getUnitInfo,
  getRoleInfo
} from '@/api'
import { Reducer, Effect } from 'umi'

export interface userListType {
  // 用户编号
  userCode: string
  //   登录账号
  accountName: string
  //   姓名
  userName: string
  //   机构名称
  unitName: string
  //   手机号
  userPhone: string
  //   学段
  grade: string
  //   角色名称
  roleName: string
  //   用户状态
  status: string
}

// 数组转树状
const arrToTree = (arr: any[]) => {
  const newArr: any[] = []
  // 1做一个映射
  const map: any = {}
  arr.forEach((item) => {
    // 为了计算方便，统一添加children
    item.children = []
    // 构建一个字典
    const key = item.code
    map[key] = item
  })

  // 2. 对于arr中的每一项
  arr.forEach((item) => {
    const parent = map[item.parentCode]
    if (parent) {
      //    如果它有父级，把当前对象添加父级元素的children中
      parent.children.push(item)
    } else {
      //    如果它没有父级（pid:''）,直接添加到newArr
      newArr.push(item)
    }
  })

  return newArr
}

interface UnitType {
  code: string
  grade: string
  name: string
  parentCode: string
  tag: string
  typeCode: string
  children?: []
}

interface RoleListType {
  // 角色编号
  code: string
  // 角色名称
  name: string
  // 备注
  desc: string
  // 状态[1:有效，0:无效]
  status: string
}

export interface UserModelState {
  userList: userListType[]
  userInfo: any
  gradeList: any[]
  unitList: any[]
  roleList: RoleListType[]
}
interface UserModelType {
  namespace: string
  state: UserModelState
  reducers: {
    setState: Reducer<UserModelState>
  }
  effects: {
    function_getUserInfo: Effect
    function_getUserInfoByCode: Effect
    function_deleteUserInfo: Effect
    function_getGradeInfo: Effect
    function_getUnitInfo: Effect
    function_getRoleList: Effect
    function_saveUserInfo: Effect
  }
}
export default <UserModelType>{
  namespace: 'userModel',
  state: {
    userList: [],
    userInfo: {},
    gradeList: [],
    unitList: [],
    roleList: []
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *function_getUserInfo({ payload: { key } }, { call, put }) {
      const result: ResultType = yield call(getUserInfo, key)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            userList: result.obj
          }
        })
      }
    },
    *function_getUserInfoByCode({ payload: { userCode } }, { call, put }) {
      const result: ResultType = yield call(getUserInfoByCode, userCode)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            userInfo: result.obj
          }
        })
      }
    },
    *function_deleteUserInfo({ payload: { userCode }, callback }, { call }) {
      const result: ResultType = yield call(deleteUserInfo, userCode)
      callback(result)
    },
    // 查询学段
    *function_getGradeInfo({}, { call, put }) {
      const result: ResultType = yield call(getGradeInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            gradeList: result.obj
          }
        })
      }
    },
    // 查询机构
    *function_getUnitInfo({ payload: { grade } }, { call, put }) {
      const result: ResultType = yield call(getUnitInfo, grade)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            unitList: arrToTree(result.obj as any[])
          }
        })
      }
    },
    *function_getRoleList(action, { call, put }) {
      const result: ResultType = yield call(getRoleInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            roleList: result.obj
          }
        })
      }
    },
    // 新增或者修改用户信息
    *function_saveUserInfo({ payload, callback }, { call }) {
      const result: ResultType = yield call(saveUserInfo, payload)
      callback(result)
    }
  }
}
